<?php
/**
 * YSEO
 * 生成 SEO 所需数据（title, keywords, description）
 * @author hiscaler <hiscaler@gmail.com>
 * @link http://www.yuandot.com/
 * @copyright Copyright &copy; 2009-2010 Yuandot Information Technology CO.,LTD.
 * @license http://doit.yuandot.com/license/
 */

class YSEO {
    /**
     * SEO 数据
     * @uses
     * $seo = YSEO::getSEO("{$words1},{$words2} .... {$wordsN}", 'title other information');
     * $this->pageTitle = $seo['title'];
     * $this->pageKeywords = $seo['keywords'];
     * $this->pageDescription = $seo['description'];
     * @param <string> $words
     * @param <string> $title_fix
     * @return array()
     */
    public static function getSEO($words = '', $title_fix = '') {
        $_title = array();
        $_keywords = array();

        $config = Yii::app()->params['frontend'];
        $site_name = trim($config['site_name']);
        $default_keywords = trim($config['default_keywords']);
        $default_description = trim($config['default_description']);

        $_title[] = $site_name;
        $_title[] = $title_fix;
        $_keywords[] = $site_name;

        if (!empty($default_keywords)) {
            $temps = explode(',', $default_keywords);
            foreach ($temps as $temp) {
                $_title[] = $temp;
                $_keywords[] = $temp;
            }
        }
        
        if (!empty($words)) {
            $temps = explode(',', $words);
            if (!empty($temps)) {
                foreach ($temps as $temp) {
                    $_title[] = $temp;
                    $_keywords[] = $temp;
                }
            }
        }

        return array(
            'title' => self::array2string($_title, ' '),
            'keywords' => self::array2string($_keywords, ','),
            'description' => trim($default_description . " {$title_fix}"),
        );
    }
    
    protected static function array2string($words, $split_char = ' ') {
        YArrayExt::removeEmpty($words, true);
        if (count($words) > 2) $words = array_unique($words);
        return (count($words)) ? implode($split_char, $words) : '';
    }
}
